package com.pro.common.api.depencies.util;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Supplier;

public class BeanMergeUtils {

    /**
     * 通用合并方法 - 传入Supplier
     */
    public static <T> void mergeFillNull(T source, T target,
                                         BiConsumer<T, T> sourceToTargetMapper,
                                         Supplier<T> instanceSupplier) {
        if (target == null || source == null || sourceToTargetMapper == null || instanceSupplier == null) {
            return;
        }

        T temp = instanceSupplier.get();
        sourceToTargetMapper.accept(source, temp);  // source → temp
        sourceToTargetMapper.accept(target, temp);  // target → temp (覆盖非空)
        sourceToTargetMapper.accept(temp, target);  // temp → target
    }

    /**
     * 批量合并 - 传入Supplier
     */
    public static <T> void mergeFillNull(T source, List<T> targets,
                                         BiConsumer<T, T> mapper,
                                         Supplier<T> instanceSupplier) {
        if (targets == null || source == null || instanceSupplier == null) return;
        targets.forEach(target ->
                mergeFillNull(source, target, mapper, instanceSupplier));
    }
}
